% Create movie
clear classes
close all

origin = 'C:\Users\del.l\Documents\UCL\data\09-04-12\';

experiment = 1;
subject = 1;
j = 1;
i = 1;

fname = [ 's' num2str(subject) 'e' ...
    num2str(experiment) 'b' num2str(j) 't' num2str(i) '.asc'];
try
    edff = ASCFILE([origin fname]);
catch err
    fprintf(1,err.message);
    continue
end
analysis = ET_ANALYSIS(edff);
analysis.refresh_rate = analysis.resample;

[s p0] = analysis.edffile.rawSignal();
st = analysis.edffile.start();
f  = analysis.edffile.final();

v = analysis.returnVectors(1);

s = analysis.edffile.interpolateSignal(s(st{1}{1}-p0:f{1}{1}-p0));
s = analysis.edffile.filter(s,60,1000);
s = s(round(1:16.6666:numel(s))); % This is very sloppy

v = (v-analysis.scn.w/2)/(analysis.scn.w/2);
s = (s-analysis.scn.w/2)/(analysis.scn.w/2);
v = (v/analysis.randomFixation());
s = (s/analysis.randomFixation());

ox = analysis.occluder.X;
ow = analysis.occluder.W;

ox = (ox)/(analysis.scn.w/2);
ow = (ow)/(analysis.scn.w/2);
ox = (ox/analysis.randomFixation());
ow = (ow/analysis.randomFixation());

% figure
%hold on
%plot(s,'r'); 
%plot(v,'k'); 
%
hf  = figure('position',[100 100 850 600]);
mov = avifile('pursuit.avi','FPS',16);
set(gca,'nextplot','replacechildren');
for i = 1:numel(s);
   cla;
   patch([ox-ow/2,ox+ow/2,ox+ow/2,ox-ow/2],[1,1,-1,-1],[0.9,0.9,0.9],'edgecolor',[0.9,0.9,0.9]);
   hold on
   hs = plot(s(i),0,'ro','MarkerSize',15);
   hv = plot(v(i),0,'ko','MarkerSize',15);
   ylim([-1,1]);
   xlim([-1.5,1.5]);
   xlabel('Normalized horizontal position')   
   legend([hs,hv],'Eye position','Stimuli','location','northeast');
   %F    = getframe(gca);
   mov = addframe(mov,gca);
end
mov = close(mov);


